package thread.demo02;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;


public class GuiTuSaiPao implements Runnable {


    private static String winner; // 胜利者

    @SneakyThrows
    @Override
    public void run() {
        for (int i = 1; i <= 1000; i++) {
            if (Thread.currentThread().getName().equals( "乌龟") && i==10) {
                Thread.sleep(10);
            }
            // 判断比赛是否结束
            boolean flag = gameOver(i);
            if (flag) {
                break;
            }
            System.out.println(Thread.currentThread().getName() + "-->跑了" + i + "步");

        }
    }

    private boolean gameOver(int steps) {
        if (winner != null) {
            return true;
        }

            if (steps >= 1000) {
                winner = Thread.currentThread().getName();
                System.out.println("winner is " + winner);
                return true;
            }

        return false;
    }

    public static void main(String[] args) {
        GuiTuSaiPao guiTuSaiPao = new GuiTuSaiPao();
        new Thread(guiTuSaiPao, "兔子").start();
        new Thread(guiTuSaiPao, "乌龟").start();
    }
}

